package com.example.tool.subject;

import com.example.tool.module.ModuleInformation;
import com.example.tool.module.ModuleInputEnum;
import com.example.tool.ui.HToolViewItem;
import com.example.tool.ui.SwingUi;
import com.example.tool.ui.SwingViewWrapper;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.openapi.fileChooser.FileChooserDescriptor;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.components.JBTextField;

import javax.swing.JComponent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HToolModuleWizardStep extends ModuleWizardStep {
  private static HToolModuleWizardStep hToolModuleWizardStep;
  private SwingViewWrapper swingViewWrapper;

  private HToolModuleWizardStep() {
  }

  public static HToolModuleWizardStep getInstance() {
    if (hToolModuleWizardStep == null) {
      hToolModuleWizardStep = new HToolModuleWizardStep();
    }
    return hToolModuleWizardStep;
  }

  public SwingViewWrapper getSwingViewWrapper() {
    return swingViewWrapper;
  }

  @Override
  public JComponent getComponent() {
    TextFieldWithBrowseButton textFieldWithBrowseButton = new TextFieldWithBrowseButton();
    FileChooserDescriptor fileChooserDescriptor = new FileChooserDescriptor(false, true, false, false, false, false);
    textFieldWithBrowseButton.addBrowseFolderListener(new TextBrowseFolderListener(fileChooserDescriptor));
    List<HToolViewItem<?>> hToolViewItems = new ArrayList<>();
    JBTextField staticDirectory = new JBTextField();
    staticDirectory.setText(ModuleInformation.DIRECTORY);
    JBTextField remotePort = new JBTextField();
    remotePort.setText(ModuleInformation.PORT);
    hToolViewItems.add(new HToolViewItem<>(ModuleInputEnum.ROOT_PATH, textFieldWithBrowseButton));
    hToolViewItems.add(new HToolViewItem<>(ModuleInputEnum.PLUG_ID, new JBTextField()));
    hToolViewItems.add(new HToolViewItem<>(ModuleInputEnum.PLUG_NAME, new JBTextField()));
    hToolViewItems.add(new HToolViewItem<>(ModuleInputEnum.PLUG_CODE, new JBTextField()));
    hToolViewItems.add(new HToolViewItem<>(ModuleInputEnum.BUILD_PATH, staticDirectory));
    hToolViewItems.add(new HToolViewItem<>(ModuleInputEnum.DEBUG_PORT, remotePort));
    swingViewWrapper = new SwingViewWrapper(null, null, ModuleInformation.TIP_CREAT, hToolViewItems);
    return swingViewWrapper.build();
  }

  @Override
  public void updateDataModel() {
  }

  @Override
  public boolean validate() throws ConfigurationException {
    Map<ModuleInputEnum, String> values = swingViewWrapper.getValues();
    for (Map.Entry<ModuleInputEnum, String> stringObjectEntry : values.entrySet()) {
      ModuleInputEnum key = stringObjectEntry.getKey();
      String value = stringObjectEntry.getValue();
      if (value == null || value.isEmpty()) {
        throw new ConfigurationException(ModuleInformation.All_INPUT);
      }
      if (key.equals(ModuleInputEnum.PLUG_ID) && !SwingUi.IsEnglish(value)) {
        throw new ConfigurationException(ModuleInformation.PLUG_ID_MSG);
      } else if (key.equals(ModuleInputEnum.PLUG_CODE) && !SwingUi.IsNumber(value)) {
        throw new ConfigurationException(ModuleInformation.PLUG_CODE_MSG);
      } else if (key.equals(ModuleInputEnum.DEBUG_PORT) && !SwingUi.IsNumber(value)) {
        throw new ConfigurationException(ModuleInformation.DEBUG_PORT_MSG);
      }
    }
    return true;
  }
}
